/*
 * $Id: ShemAlarmLogReader.java,v 1.3 2009/07/10 20:02:16 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.shemjob.reader;

import java.io.InputStream;

import com.ge.healthcare.autosc.processor.exception.MagnetDataFormatException;
import com.ge.healthcare.autosc.processor.exception.ShemReaderException;
import com.ge.healthcare.autosc.processor.magmon.IMagnetMonitorProcessor;
import com.ge.healthcare.autosc.processor.magmon.MagnetMonitorUtility;

/**
 * An alarm log reader. The source is an alarm log, typically from a request.
 * The returned lines are in magnet alarm log format.
 * 
 * @author Daniel S. Horwitz, Compuware Corp.
 * 
 * @version AutoSC P5P6
 */
public class ShemAlarmLogReader extends ShemSimpleTextReader {
	/**
	 * Utility class for the magnet monitor.
	 */
	private MagnetMonitorUtility theUtility = null;

	/**
	 * Name used to identifier the reader.
	 */
	private String theReaderId = null;

	/**
	 * Create an alarm log reader.
	 */
	public ShemAlarmLogReader() {
		super();
	}

	/**
	 * Open the text reader.
	 * 
	 * @param readerId
	 *            Name used to identifier the reader.
	 * 
	 * @param rawSource
	 *            The raw input stream for reading the data. It is the
	 *            responsibility of the caller to make sure that this is
	 *            buffered.
	 * 
	 * @param helper
	 *            Object providing the utility objects and methods.
	 * 
	 * @param utility
	 *            Utility class with magnet monitor methods.
	 * 
	 * @param magmon
	 *            The magnet monitor, which is used to get shem type and
	 *            parameter configuration information for configurable readers.
	 */
	public void open(String readerId, InputStream rawSource,
			MagnetMonitorUtility utility,
			IMagnetMonitorProcessor magMonProcessor) {
		super.open(readerId, rawSource, utility, magMonProcessor);
		theUtility = utility;
		theReaderId = readerId;
	}

	/**
	 * Read a line of text. This returns the next high limit or low limit record
	 * from the alarm log, translated to magnet alarm log format.
	 * 
	 * @return A string containing the next high limit or low limit record
	 *         available, in magnet alarm log format.
	 * 
	 * @exception AscServiceAbortException
	 *                If an error is detected reading a line of text, an error
	 *                is logged and then an abort exception is thrown.
	 */
	public String readLine() throws ShemReaderException {
		for (String currentLine = super.readLine(); currentLine != null; currentLine = super
				.readLine()) {
			String translatedLine = null;
			try {
				translatedLine = theUtility.getAlarmLineFrom(currentLine);
			} catch (MagnetDataFormatException formatError) {
				String formatSubject = new StringBuffer().append(
						"Error detected reading ").append(theReaderId).append(
						".  ").append(formatError).toString();
				throw new ShemReaderException(formatSubject);
			}
			if (translatedLine != null) {
				return translatedLine;
			}
		}
		return null;
	}
}
